/*
 * Copyright 2007 Jeffrey Palm
 */

import java.io.*;
import java.util.*;
import javax.microedition.io.*;
import javax.microedition.lcdui.*;
import javax.microedition.lcdui.List;
import javax.microedition.midlet.*;

public class FeedListList extends AbstractList {

  void ok(final Object value, final Bloglines midlet) {
    final Feed f = (Feed)value;
    new Thread(new Runnable() {
	public void run() {
	  try {
	    midlet.showProgress("Searching","Searching " + f.getTitle() + "...");
	    Vector subs = midlet.request("getitems?s=" + f.getProperty(ID));
	    FeedList lst = new FeedList(midlet,FeedListList.this,f);
	    lst.parse(subs);
	    midlet.setCurrent(lst);
	  } catch (Exception e) {e.printStackTrace();}
	}}).start();
  }

  FeedListList(final Bloglines midlet) {
    super(midlet,"Bloglines");
  }

  private final Vector feeds = new Vector();
  
  private final static String ID = "id";

  void parseRest(Vector subs) {
    for (int i=0; i<subs.size(); i++) {
      String line = String.valueOf(subs.elementAt(i));
      if (line == null) continue;
      line = line.trim();
      // <outline title="Example Blog 1" htmlUrl="http://example.com" 
      // type="rss" xmlUrl="http://example.com/rss.xml"  BloglinesSubId="2563871" 
      // BloglinesUnread="0" BloglinesIgnore="1" />
      if (line.startsWith("<outline") && line.endsWith(" />")) {
	String title = Util.getAttribute("title",line);
	String htmlUrl = Util.getAttribute("htmlUrl",line);
	String type = Util.getAttribute("type",line);
	String xmlUrl = Util.getAttribute("xmlUrl",line);
	String unread = Util.getAttribute("BloglinesUnread",line);
	String id = Util.getAttribute("BloglinesSubId",line);
	Feed f = new Feed(xmlUrl,title);
	f.setProperty(ID,id);
	String str = "(" + unread + ") " + title;
	append(f,str);
      }
    }
  }

}
